package cn.HL.org.service.impl;

import cn.HL.basic.exception.BusinessException;
import cn.HL.basic.util.BaiduAuditUtils;
import cn.HL.org.domain.Employee;
import cn.HL.org.domain.Shop;
import cn.HL.org.domain.ShopAuditLog;
import cn.HL.org.mapper.EmployeeMapper;
import cn.HL.org.mapper.ShopAuditLogMapper;
import cn.HL.org.mapper.ShopMapper;
import cn.HL.org.service.IEmployeeService;
import cn.HL.org.service.IShopService;
import cn.HL.basic.service.impl.BaseServiceImpl;
import cn.HL.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HL
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 1.判断输入是否为空
     * 2.判断是否合法
     * 3.判断商铺名是否被注册
     * 4.判断店长是否注册
     *   4.1判断是否绑定过商铺
     * 5.保存数据集
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        //1.接收shop中的子类后面用
        Employee admin = shop.getAdmin();
        //判断后端传过来的数据是否为空
        if(StringUtils.isEmpty(shop.getName())
                ||StringUtils.isEmpty(shop.getAddress())
                ||StringUtils.isEmpty(admin.getUsername())
        ){
            //为空我就抛异常
            throw new BusinessException("参数不能填空哦");
        }
        //百度的AI审核文本
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("小伙子你有点危险哦");
        }

        String logo = shop.getLogo();
        String logos="http://123.207.27.208"+logo;
        if(!BaiduAuditUtils.ImgCensor(logos)){
            throw new BusinessException("小伙子你有点不健康哦");
        }
        //参数不为空就判断商铺名是否被注册
        Shop shopTem = shopMapper.loadByName(shop.getName());
        if(shopTem!=null){
            //进去这里就说明商户名存在了
            throw new BusinessException("商户名已经存在不要填这个了哦");
        }
        //不存在我查看他的管理员是否注册过
        Employee empTem = employeeMapper.loadByUserName(admin.getUsername());
        if(empTem!=null&&empTem.getShop_id()!=null){
            //进入这里说明他管理存在而且注册了商铺
            throw new BusinessException("管理员已经存在了叫他滚了!");
        }
        //判断一下然后新增
        if(empTem==null){
            //管理员等于空我就新增 然后获取自增id
            employeeService.save(admin);
            empTem=admin;
        }
        //获取员工的id然后添加到店铺去 然后再新增
        shop.setAdminId(empTem.getId());
        shopMapper.save(shop);
        //获取商铺的id 然后修改员工的商铺id
        empTem.setShop_id(shop.getId());
        employeeService.update(empTem);
    }

    /**
     *
     * @param shopAuditLog
     */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        //1.修改审核状态
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(1);
        shopMapper.update(shop);
        //获取管理员信息
        Long shopId = shopAuditLog.getShopId();
        Employee employee= employeeMapper.LoadByShopid(shopId);
        shopAuditLog.setAuditId(employee.getId());
        //上传日志
        shopAuditLog.setState(1);
        shopAuditLogMapper.save(shopAuditLog);
        //发送邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("895730310@qq.com");
        //邮件主题
        mailMessage.setSubject("宠物乐园");
        //邮件内容
        mailMessage.setText("恭喜你审核通过了!请尽快激活,点击链接即可激活:http://localhost:8080/shop/activation/"+shopAuditLog.getShopId());
        //收件人
        mailMessage.setTo(employee.getEmail());
        javaMailSender.send(mailMessage);
    }

    /**
     *审核驳回
     * @param shopAuditLog
     */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        //1.修改审核状态
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(-1);
        shopMapper.update(shop);
        //获取管理员信息
        Employee employee= employeeMapper.LoadByShopid(shopAuditLog.getShopId());
        shopAuditLog.setAuditId(employee.getId());
        //上传日志
        shopAuditLog.setState(-1);
        shopAuditLogMapper.save(shopAuditLog);
        //发送邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("895730310@qq.com");
        //邮件主题
        mailMessage.setSubject("宠物乐园");
        //邮件内容
        mailMessage.setText("你的申请不合规请重新填写再提交,点此连接修改:http://localhost:8081/#/ShopRegister/"+shopAuditLog.getShopId());
        //收件人
        mailMessage.setTo(employee.getEmail());
        javaMailSender.send(mailMessage);
    }

    @Override
    public void batchSave(List<Shop> shops) {
        shopMapper.batchSave(shops);

    }

    @Override
    public List<ShopVo> findEcharts() {
        List<ShopVo>shopList = shopMapper.findEcharts();

        return shopList;
    }

    /**
     * 激活
     * @param id
     */
    @Override
    public void activation(Long id) {
        Shop shop = shopMapper.loadById(id);

        shop.setState(2);
        shopMapper.update(shop);



    }


}
